package com.bmf.admin.module.scenario.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bmf.admin.constant.AdminResultEnum;
import com.bmf.admin.module.scenario.pojo.ScenarioComponents;
import com.bmf.admin.module.scenario.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Objects;


/**
 * 想定(Scenario)表服务实现类
 *
 * @author makejava
 * @since 2024-08-06 11:58:44
 */
@Service("scenarioService")
@Slf4j
public class ScenarioServiceImpl implements ScenarioService {

    @Resource
    private RestTemplate restTemplate;

    @Value("${api.url}")
    private String resourceBaseUrl;

    @Override
    public ScenarioComponents getScenarioById(String id) {
        //发送请求获取想定数据
        String url = resourceBaseUrl + "/scenario/detail/"+id;
        // 响应结果
        String responseString = "";
        try {
            responseString = restTemplate.getForObject(url, String.class);
        } catch (Exception e) {
            log.error("Error occurred while fetching scenario by ID: {}", id, e);
            AdminResultEnum.SCENARIO_RESULT_EXCEPTION.assertFail();
        }
        // 响应结果解析
        JSONObject jsonObject = JSON.parseObject(responseString);
        // 获取code字段的值作为初始化结果
        Integer initResult = jsonObject.getInteger("code");
        // 断言初始化结果为200
        AdminResultEnum.SCENARIO_RESULT_EXCEPTION.assertIsTrue(Objects.equals(200, initResult));
        // 响应结果解析
        return JSON.parseObject(jsonObject.getString("result"), ScenarioComponents.class);
    }

    @Override
    public Object receLoadingSignal(Object scenarioResultVo) {
        log.info("收到想定加载信号");
        return null;
    }

    @Override
    public Object scenarioStart(String id) {
        log.info("想定开始");
        return null;
    }

    @Override
    public Object scenarioPause(String id) {
        log.info("想定暂停");
        return null;
    }

    @Override
    public Object adjustScenarioStepping(String id, String step) {
        log.info("调整想定步进");
        return null;
    }

    @Override
    public Object endOfScenario(String id) {
        log.info("结束想定");
        return null;
    }

    @Override
    public Object saveBreakpoint(Object createVo) {
        log.info("保存断点");
        return null;
    }

    @Override
    public Object restoreBreakpoint(String id, String breakpointId) {
        log.info("断点恢复");
        return null;
    }

}
